package leetcode.n2022;

import java.util.Arrays;

/**
 * 27. 移除元素
 * https://leetcode-cn.com/problems/remove-element/
 *
 * @Author: Chaos
 * @Date: 2022/1/21
 */
public class RemoveElement {

    public int removeElement(int[] nums, int val) {
        int a = nums.length;
        int i = 0;
        while(i < nums.length) {
            if (nums[i] == val) {
                int j = i;
                a--;
                while(j < nums.length) {
                    if (j == nums.length - 1) {
                        nums[j] = -1;
                    } else {
                        nums[j] = nums[j+1];
                    }
                    j++;
                }
            } else {
                i++;
            }
        }

        return a;
    }

    public static void main(String[] args) {
        RemoveElement r = new RemoveElement();

        int[] nums = {4, 4, 0, 1,0,2};
        int v = 0;

        System.out.println(r.removeElement(nums, v));
        System.out.println(Arrays.toString(Arrays.stream(nums).toArray()));
    }
}
